﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.Security;


namespace SVAccountingBL
{
    [DataObject(true)]
    public class UsersODS
    {
        SVALinqDataContext db = new SVALinqDataContext();


        
        #region CompanyAccountantsMethdos
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public List<Company> GetAccountnatCompanies(string userName)
        {

            if (!string.IsNullOrEmpty(userName))
            {
                MembershipUser ChoosenUser = Membership.GetUser(userName);
                Person thePerosn = (from comp in db.Persons
                                    where comp.UserName == userName
                                    select comp).Single();

                List<Company> result = (from comp in db.Companies
                                        join
                                            rAccPer in db.rAccountantPersons on comp.CompanyID equals rAccPer.CompanyID
                                        where rAccPer.PersonID == thePerosn.PersonID
                                        select comp).ToList();

                return result;
            }
            else
                return null;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public bool RemoveCompanyFromAccountant(Company cpToRemove)
        {
            try
            {
                rAccountantPerson theRelation = db.rAccountantPersons.Single(p => (p.Person.UserName == cpToRemove.rAccountantPersons[0].Person.UserName) && (p.CompanyID == cpToRemove.CompanyID));
                db.rAccountantPersons.DeleteOnSubmit(theRelation);
                db.SubmitChanges();

            }
            catch (Exception exx)
            {
                return false;
            }
            return true;
        }

        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public bool AssosiateCompanyToAccountatn(string userName, string companyID)
        {
            try
            {
                rAccountantPerson theRelation = new rAccountantPerson();
                Person thePerson = (from p in db.Persons
                                    where p.UserName == userName
                                    select p).Single();

                theRelation.Person = thePerson;
                theRelation.PersonID = thePerson.PersonID;
                theRelation.CompanyID = int.Parse(companyID);
                db.rAccountantPersons.InsertOnSubmit(theRelation);
                db.SubmitChanges();
            }
            catch (Exception exx)
            {
                return false;
            }
            return true;
        }
        #endregion

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public List<UserWraper> GetAllUsers()
        {
            List<UserWraper> AllUsersInSystem = new List<UserWraper>();
            foreach (MembershipUser item in Membership.GetAllUsers())
            {
                UserWraper temp = GetUserInfo(item.UserName);
                if (temp != null)
                    AllUsersInSystem.Add(temp);
            }

            return AllUsersInSystem;
        }


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public List<Company> GetFreeCompaniesForAccounant(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                MembershipUser ChoosenUser = Membership.GetUser(userName);
                Person thePerosn = (from comp in db.Persons
                                    where comp.UserName == userName
                                    select comp).Single();

                List<Company> result = (from t in db.Companies
                                        where
                                        !(from t0 in db.rAccountantPersons
                                          where t0.PersonID == thePerosn.PersonID
                                          select new
                                          {
                                              t0.CompanyID
                                          }).Contains(new { t.CompanyID })
                                        select t).ToList();
                return result;
            }
            else
                return null;
        }


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public UserWraper GetUserInfo(string userName)
        {
            SVALinqDataContext db = new SVALinqDataContext();
            UserWraper TheUser = new UserWraper();
            MembershipUser ChoosenUser = Membership.GetUser(userName);


            TheUser.UserName = userName;
            TheUser.IsOnline = ChoosenUser.IsOnline;
            TheUser.Email = ChoosenUser.Email;
            TheUser.IsApproved = ChoosenUser.IsApproved;
            TheUser.IsLockedOut = ChoosenUser.IsLockedOut;
            string[] UserRoles = Roles.GetRolesForUser(userName);
            if (UserRoles.Length == 1)
                TheUser.UserRoleName = UserRoles[0];

            try
            {
                Person thePerosn = (from comp in db.Persons
                                    where comp.UserName == userName
                                    select comp).Single();

                TheUser.UserID = thePerosn.PersonID;
                TheUser.FirstName = thePerosn.FirstName;
                TheUser.LastName = thePerosn.SecoundName;

                List<Company> theOwnCompanies = (from comp in db.Companies
                                                 join
                                                     rAccOwn in db.rCompanyOwners on comp.CompanyID equals rAccOwn.CompanyID
                                                 where rAccOwn.PersonID == thePerosn.PersonID
                                                 select comp).ToList();

                
            }
            catch (Exception exx)
            {
                return null;
            }

            return TheUser;
        }

        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public bool UpdateUserInfo(UserWraper theUser)
        {
            return false;
        }

        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public bool DeleteUser(UserWraper theUser)
        {
            return false;
        }

        [DataObjectMethod(DataObjectMethodType.Insert, true)]
        public bool InsertUser(UserWraper theUser)
        {
            return false;
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static List<RolesBinding> Rolles()
        {
            List<RolesBinding> AllRolesForBinding = new List<RolesBinding>();
            RolesBinding RoleBinding = new RolesBinding();

            RoleBinding = new RolesBinding();
            RoleBinding.RoleName = null;
            AllRolesForBinding.Add(RoleBinding);

            foreach (string RoleToBind in Roles.GetAllRoles())
            {
                RoleBinding = new RolesBinding();
                RoleBinding.RoleName = RoleToBind;
                AllRolesForBinding.Add(RoleBinding);
            }
            return AllRolesForBinding;


        }

    }

    public class UserWraper
    {
        public int UserID { get; set; }

        public string UserName { get; set; }

        public string Email { get; set; }

        public bool IsApproved { get; set; }

        public string UserRoleName { get; set; }

        public bool IsOnline { get; set; }

        public bool IsLockedOut { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public UserWraper()
        {

        }

    }

    public class RolesBinding
    {
        public string RoleName { get; set; }

    }
}
